package 
{
    public class HashMap extends Object
    {
        protected var map:Object;
        protected var index:Array = [];
        
        public function HashMap()
        {
            map = new Object();
        }

        public function put(key:*, value:*) : void
        {
            map[key] = value;
			index.push(key);
        }
        
		/**
		 * Removes the item from the Hashmap
		 * @param	key
		 * @return
		 */
        public function remove(value:*) : Boolean
        {
			return removeKey(getKey(value));
        }
		/**
		 * Removes the item from the Hashmap that corresponds to the given key
		 * @param	key
		 * @return
		 */
        public function removeKey(key:*) : Boolean
        {
			var wasDeleted:Boolean = delete map[key];
			
			if (wasDeleted) 
			{
				index.splice(index.indexOf(key), 1);
			}
			return wasDeleted;
        }

        public function containsKey(key:*) : Boolean
        {
            //return map.hasOwnProperty( key );
			return index.indexOf(key) != -1;
        }

        public function containsValue(value:*) : Boolean
        {
            for (var key:* in map)
                if (map[key] == value)
                    return true;
					
            return false;
        }

        public function getKey(value:*) : *
        {
            for ( var key:* in map )
            {
                if ( map[key] == value )
                {
                    return key;
                }
            }
			return false;
        }

        public function getKeys() : Array
        {
			return index;
			/*
            var keys:Array = [];

            for (var key:* in map)
                keys.push( key );
				
            return keys;
			*/
        }

        public function getValue(key:*) : *
        {
            return map[key];
        }

        public function getValues() : Array
        {
            var values:Array = [];

            for each(var key:* in index)
                values.push( map[key] );

			return values;
        }

        public function size() : int
        {
			return index.length;
			/*
            var length:int = 0;

            for (var key:* in map)
                length++;

			return length;
			*/
        }

        public function isEmpty() : Boolean
        {
            return size() <= 0;
        }

        public function clear() : void
        {
			map = new Object();
			index = [];
        }
		
		public function toString():String
		{
			var str:String = 'HashMap[';
            for each(var key:* in index)
                str += key +' -> ' + map[key] +',';
			return str + ']';
		}
    }
}